package com.googlecode.ozonbooksfinder.cairngormfinder.command
{
	import com.adobe.cairngorm.commands.ICommand;
	import com.adobe.cairngorm.commands.SequenceCommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	import com.googlecode.ozonbooksfinder.cairngormfinder.event.GetFavoritesEvent;
	import com.googlecode.ozonbooksfinder.cairngormfinder.event.SaveFavoriteEvent;
	import com.googlecode.ozonbooksfinder.cairngormfinder.model.CairngormFinderModelLocator;
	import com.googlecode.ozonbooksfinder.domain.Book;
	
	import flash.net.SharedObject;
	
	import mx.collections.ArrayCollection;
	
	public class SaveFavoriteCommand extends SequenceCommand implements ICommand
	{
		private var model:CairngormFinderModelLocator = CairngormFinderModelLocator.getInstance();
		
		private var favoritesSO:SharedObject = SharedObject.getLocal("favorites");
		
		override public function execute(event:CairngormEvent):void
	   	{
	   		var evt:SaveFavoriteEvent = SaveFavoriteEvent(event); 
	   		var book:Book = evt.book;
	   		
	   		var favorites:ArrayCollection = new ArrayCollection ();
			
			if (favoritesSO.data.favorites != null)
			{
				favorites.source = favoritesSO.data.favorites as Array;
			}
			
	   		favorites.source.push(book);
	   		favoritesSO.data.favorites = favorites.source;
			favoritesSO.flush();
			
			nextEvent = new GetFavoritesEvent();
			executeNextCommand();
	   	}
	}
}